package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.service.ActivityModuleConfigService;
import cn.com.connext.scrm.service.IndexModuleConfigService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.web.rest.vm.*;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 第三方调用接口
 */
@RestController
@RequestMapping("/api/indexmoduleconfig/")
public class IndexModuleConfigResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(IndexModuleConfigResource.class);
    @Autowired
    private IndexModuleConfigService imcService;


    public IndexModuleConfigResource() {
        super("IndexModuleConfig");
    }

    /**
     * 新页面活动创建
     */
    @ApiOperation(value = "新页面活动创建", notes = "新页面活动创建")
    @PostMapping(value = "/addSecondActivity")
    public JsonResult addSecondActivity(@RequestBody SecondActivity entity ){
        return  imcService.addSecondActivity(entity);
    }

    /**
     * 新页面活动删除
     */
    @ApiOperation(value = "新页面活动创建", notes = "新页面活动创建")
    @PostMapping(value = "/deleteSecondActivity")
    public JsonResult deleteSecondActivity(@RequestBody SecondActivity entity ){
        return  imcService.deleteSecondActivity(entity);
    }

    /**
     * 新页面活动删除
     */
    @ApiOperation(value = "分页查询新活动列表", notes = "分页查询新活动列表")
    @PostMapping(value = "/findByPage")
    public JsonResult findByPage(@Valid @RequestBody MemberIndexModulePageVM vm){
        JsonResult result =new JsonResult();
        try{
            if(vm ==null || vm.getPage() ==null || vm.getPageSize() ==null){
                result.setState(-1);
                result.setMessage("参数无能为空");
                return  result;
            }
            Page<SecondActivity> page = imcService.findByPage(vm);
            result.setData(page);
            result.setState(0);
        }catch (Exception e){
            result.setMessage("系统异常");
            result.setState(-1);
            log.info("异常信息------------"+e.getMessage());
        }
        return  result;
    }


    /**
     * 新增活动组件
     */
    @ApiOperation(value = "新增活动组件", notes = "新增活动组件")
    @PostMapping(value = "/add")
    public JsonResult add(@RequestBody IndexModuleConfigAddVM vm ){
        return  imcService.save(vm);
    }


    /**
     * 编辑组件
     */
    @ApiOperation(value = "编辑组件", notes = "编辑组件")
    @PostMapping(value = "/editCoupon")
    public JsonResult editCoupon(@RequestBody IndexModuleConfigEditVM vm){
        JsonResult result = new JsonResult();
        try{
            result = imcService.editCoupon(vm);
        } catch (Exception e) {
            log.error(e.getMessage());
            result.setState(-1);
            result.setMessage("系统异常,查询失败");
        }
        return result;
    }

    /**
     * 查询组件
     */
    @ApiOperation(value = "查询组件", notes = "查询组件")
    @PostMapping(value = "/findById")
    public JsonResult findById(@RequestBody IndexModuleConfig vm){
        JsonResult result = new JsonResult();
        try{
            result = imcService.findById(vm.getId());
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("系统异常,查询失败");
        }
        return result;
    }

    /**
     * 新增活动组件
     */
    @ApiOperation(value = "删除活动组件", notes = "删除活动组件")
    @PostMapping(value = "/deleteComponent")
    public JsonResult deleteComponent(@RequestBody IndexModuleConfigVM vm ){
        return  imcService.deleteComponent(vm);
    }


    /**
     * 查询所有组件
     */
    @ApiOperation(value = "查询所有组件", notes = "查询所有组件")
    @PostMapping(value = "/findAllModule")
    public JsonResult findAllModule(){
        JsonResult result = new JsonResult();
        try{
            result = imcService.findAllModule();
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }


    /**
     * 新增活动配置模块
     */
    @ApiOperation(value = "新增活动配置模块", notes = "新增活动配置模块")
    @PostMapping(value = "/addSecondActivityModule")
    public JsonResult addSecondActivityModule(@RequestBody IndexModule entity ){
        return  imcService.addIndexModule(entity);
    }

    /**
     * 新增活动配置模块list
     */
    @ApiOperation(value = "新增活动配置模块list", notes = "新增活动配置模块list")
    @PostMapping(value = "/addIndexModuleList")
    public JsonResult addIndexModuleList(@RequestBody IndexModuleVM vm ){
        return  imcService.addIndexModuleList(vm);
    }

    /**
     * 查询当前活动配置的模块
     */
    @ApiOperation(value = "查询该活动已配置的组件", notes = "查询该活动已配置的组件")
    @PostMapping(value = "/findModuleBySecondActivityId")
    public JsonResult findModuleBySecondActivityId(@RequestBody IndexModule entity){
        JsonResult result = new JsonResult();
        try{
            result = imcService.findModuleBySecondActivityId(entity.getSecondActivityId());
        } catch (Exception e) {
            log.info("异常信息: {}",e.getMessage());
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }


    /**
     * 删除该活动已配置的组件
     */
    @ApiOperation(value = "删除该活动已配置的组件", notes = "删除该活动已配置的组件")
    @PostMapping(value = "/delete")
    public JsonResult delete(@RequestBody IndexModule entity){
        JsonResult result = new JsonResult();
        try{
            result = imcService.delete(entity);
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("系统异常,删除失败");
        }
        return result;
    }

    /**
     * 记录老会员今日是否有过弹窗
     */
    @ApiOperation(value = "记录老会员今日是否有过弹窗", notes = "记录老会员今日是否有过弹窗")
    @PostMapping(value = "/recordOpenWindowByUnionId")
    public JsonResult recordOpenWindowByUnionId(@RequestBody RecordOpenWindow entity){
        JsonResult result = new JsonResult();
        try{
            result = imcService.recordOpenWindowByUnionId(entity);
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("系统异常");
        }
        return result;
    }


}

